package com.info.dbase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;
/**
 * @author HAHA
 * 
 */
public class ConnectionManager {
	private static Logger logger=Logger.getLogger(ConnectionManager.class);
	public static String dbUser = "root";
	public static String dbPass = "123";
	public static String dbaddress = "127.0.0.1";
	public static String dbname = "wulianwang";
	public static String dbport = "3306";
	public static String dbDriver = "org.gjt.mm.mysql.Driver";
	private static ConnectionManager instance = null;
	private static Connection conn = null;

	/**
	 * 获取连接管理器
	 * @return
	 */
	public static ConnectionManager getInstance() {
		if (instance == null)
			instance = new ConnectionManager();
		return instance;
	}

	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Class.forName(dbDriver);

			conn = DriverManager.getConnection("jdbc:oracle:thin:@" + dbaddress
					+ ":" + dbport + ":" + dbname, dbUser, dbPass);
			return conn;
		} catch (ClassNotFoundException e) {
			logger.info("get connection error!");
			logger.info(e);
		} catch (SQLException e) {
			logger.info("get connection error!");
			logger.info(e);
		}

		return null;
	}
	
	/**
	 * 关闭连接
	 * @throws SQLException
	 */
	public void close() throws SQLException {
		conn.close();
	}

	public static void main(String[] arg) {
		ConnectionManager sm = new ConnectionManager();
		getConnection();
		logger.info("OK");
	}
}
